#!/bin/bash

# set hostname
# echo k8s-master > /etc/hostname

# 永久关闭swap分区
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab

echo $1 > /etc/hostname
systemctl restart systemd-hostnamed
sleep 1
echo "==set hostname: `hostname`"

# add hostname to hosts
cp /etc/hosts /etc/hosts.bak
echo "127.0.0.1 `hostname`" >> /etc/hosts

# 禁用防火墙,vim /usr/bin/firewall-cmd python --> python2
echo "==disable firewalld:"
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
echo "firewall state: "
firewall-cmd --state
echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables


echo "==config kubernetes of  the Aliyun repo "
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 安装kubeadm、kubectl、kubelet
echo "==install kubeadm、kubectl、kubelet: "
# yum install -y kubectl kubeadm kubelet
yum install -y kubectl-1.21.9 kubeadm-1.21.9 kubelet-1.21.9
# 启动kubelet服务
systemctl enable kubelet && systemctl start kubelet
kubeadm version
kubectl version --client
kubelet --version

#
echo "==pull and rename images: "
#./pullimages.sh

kubeadm init  --kubernetes-version=v1.21.9 --ignore-preflight-errors=NumCPU --upload-certs | tee kubeadm-init.log

#简化linux命令
echo alias k="kubectl" >> ~/.bashrc
source ~/.bashrc

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# kubeadm join 192.168.0.243:6443 --token d8fl1w.utw6yqp74r2chz5q \
	# --discovery-token-ca-cert-hash sha256:15e9f975a8d813359eee305181610eb18d1db5592452af7d52742e1611435a4b
	
	
